<?php

namespace catchAdmin\phone\model;

use catchAdmin\model\Notice;
use catcher\exceptions\AlertErrorException;

class LiuYan extends \catchAdmin\model\LiuYan
{

    /**
     * 保存
     */
    public function saveData($param)
    {
        if ($this->getLoginUser()['company_status'] != 3) {
            throw new AlertErrorException('未通过审核，不能留言');
        }

        $this->startTrans();

        // 保存
        $msg = $this->saveDataApp($param);

        if ($this
                ->where('to_id', 0)
                ->where('wechat_user_id', $msg['wechat_user_id'])
                ->where('created_at', ">=", strtotime(date('Y-m-d 00:00:00')))
                ->where('created_at', "<=", strtotime(date('Y-m-d 23:59:59')))
                ->count() > 50) {
            throw new AlertErrorException('留言失败，每人每天最多发5条');
        }

        // 2.发消息
        (new Notice())->saveDataCC([
            // 留言——待审核
            'notice_type'    => 6,
            'title'          => '待审核',
            'content'        => "等待后台审核",
            'wechat_user_id' => $msg['wechat_user_id'],
        ]);

        $this->commit();

        return 1;
    }


    /**
     * 回复
     */
    public function huiFu($param)
    {
        if ($this->getLoginUser()['company_status'] != 3) {
            throw new AlertErrorException('未通过审核，不能回复');
        }

        $this->saveDataApp($param);

        return 1;
    }

    ######################################## 显示 ################################################


    /**
     * 列表
     */
    public function indexData($param)
    {
        $app = $this
            ->where('to_id', 0)
            ->where("audit_status", 2)
            ->with(['wechatUser']);

        $list = $this->indexDataCC($param, $app);

        $list_2 = [];
        foreach ($list['list'] as $k => $v) {
            $tmp = [
                'username'    => $v['wechatUser']['name'] ?? '',
                'id'          => $v['id'],
                'zhu_ti_name' => $v['zhu_ti_name'],
                'title'       => $v['title'],
                'content'     => $v['content'],
                'time'        => formatRelativeTime(strtotime($v['created_at'])),
            ];

            // 是否点赞
            $tmp['zan_is'] = 0;
            if ((new \catchAdmin\model\LiuYanZan())
                    ->where([
                        'liu_yan_id'     => $v['id'],
                        'wechat_user_id' => $this->getLoginId(),
                    ])
                    ->count() > 0) {
                $tmp['zan_is'] = 1;
            }

            $list_2[] = $tmp;
        }
        $list['list'] = $list_2;

        return $list;
    }


    public function readData($param)
    {
        $msg = $this
            ->where('id', $param['id'])
            ->with(['wechatUser'])
            ->find()
            ->toArray();

        $msg['username'] = $msg['wechatUser']['name'] ?? '';
        unset($msg['wechatUser']);

        // 回复
        $msg['hui'] = [];
        $hui_list = $this
            ->where('id', ">", $msg['id'])
            ->where('num_no', $msg['num_no'])
            ->with(['wechatUser'])
            ->select();
        if (!empty($hui_list)) {
            foreach ($hui_list as $k2 => $v2) {
                $msg['hui'][] = [
                    'username' => $v2['wechatUser']['name'] ?? '',
                    'content'  => $v2['content'],
                    'time'     => formatRelativeTime(strtotime($v2['created_at'])),
                ];
            }
        }

        $msg['time'] = formatRelativeTime(strtotime($msg['created_at']));

        // 是否点赞
        $msg['zan_is'] = 0;
        if ((new \catchAdmin\model\LiuYanZan())
                ->where([
                    'liu_yan_id'     => $msg['id'],
                    'wechat_user_id' => $this->getLoginId(),
                ])
                ->count() > 0) {
            $msg['zan_is'] = 1;
        }

        return $msg;
    }


    public function indexZhuTi()
    {
        $list = [];

        foreach ($this->getZhuTiList() as $k => $v) {
            $list[] = [
                'id'   => $k,
                'name' => $v,
            ];
        }

        return $list;
    }

}
